package com.gzy.mylog.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class SPUtils {
    private static String TYPE = "data";

    public static void save(Context context, String key, String value){
        //步骤1：创建一个SharedPreferences对象
        SharedPreferences sharedPreferences= context.getSharedPreferences(TYPE, Context.MODE_PRIVATE);
        //步骤2： 实例化SharedPreferences.Editor对象
        SharedPreferences.Editor editor = sharedPreferences.edit();
        //步骤3：将获取过来的值放入文件
        editor.putString(key, value);
        //步骤4：提交
        editor.commit();
    }

    public static String read(Context context,String key){
        SharedPreferences sharedPreferences=  context.getSharedPreferences(TYPE, Context .MODE_PRIVATE);
        return sharedPreferences.getString(key,"");
    }
    public static void remove(Context context, String key){
        //步骤1：创建一个SharedPreferences对象
        SharedPreferences sharedPreferences= context.getSharedPreferences(TYPE, Context.MODE_PRIVATE);
        //步骤2： 实例化SharedPreferences.Editor对象
        SharedPreferences.Editor editor = sharedPreferences.edit();
        //步骤3：将获取过来的值放入文件
        editor.remove(key);
        //步骤4：提交
        editor.commit();
    }   public static void clear(Context context){
        //步骤1：创建一个SharedPreferences对象
        SharedPreferences sharedPreferences= context.getSharedPreferences(TYPE, Context.MODE_PRIVATE);
        //步骤2： 实例化SharedPreferences.Editor对象
        SharedPreferences.Editor editor = sharedPreferences.edit();
        //步骤3：将获取过来的值放入文件
        editor.clear();
        //步骤4：提交
        editor.commit();
    }
}
